﻿namespace WpInsights.Core
{
    using System.IO;
    using System.Xml;
    using System.Xml.Serialization;

    static class Extensions
    {
        public static string SerializeObject<T>(this T toSerialize)
        {

            var settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;

            var ms = new MemoryStream();
            XmlWriter writer = XmlWriter.Create(ms, settings);

            var names = new XmlSerializerNamespaces();
            names.Add("", "");

            XmlSerializer cs = new XmlSerializer(toSerialize.GetType());
            cs.Serialize(writer, toSerialize, names);

            ms.Flush();
            ms.Seek(0, SeekOrigin.Begin);
            StreamReader sr = new StreamReader(ms);
            var result = sr.ReadToEnd();
            return result;
        }
    }
}
